package hello.reddwarf.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * user
 * 01.02.12 14:59
 */
public class ClientFrame extends JFrame {

    private JTextField usernameField;
    private JLabel scoreLabel;
    private JButton loginButton;
    private JButton playButton;
    private JLabel statusLabel;
    private JButton rockButton;
    private JButton paperButton;
    private JButton scissorsButton;
    private Client client;

    public static void main(String[] args) {
         new ClientFrame("RPS").setVisible(true);
     }

    protected ClientFrame(String title) {
        super(title);
        Container c = getContentPane();
        c.setLayout(new GridLayout(10,1));

        c.add(new JLabel("Username:"));
        c.add(usernameField = new JTextField("player"+Math.round(Math.random()*10)));
        c.add(scoreLabel = new JLabel("SCORE"));
        c.add(loginButton = new JButton("Log in"));
        c.add(playButton = new JButton("Play!"));
        c.add(statusLabel = new JLabel("Status"));
        c.add(rockButton = new JButton("Rock"));
        c.add(paperButton = new JButton("Paper"));
        c.add(scissorsButton = new JButton("Scissors"));

        loginButton.setEnabled(true);
        playButton.setEnabled(false);
        rockButton.setEnabled(false);
        paperButton.setEnabled(false);
        scissorsButton.setEnabled(false);

        setSize(320, 240);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setStatus("Logging in...");
                client = new Client("localhost", usernameField.getText(), ClientFrame.this);
                client.login();
            }
        });
        playButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setStatus("Playing...");
                client.play();
            }
        });
        rockButton.addActionListener(new SendActionListener("ROCK"));
        paperButton.addActionListener(new SendActionListener("PAPER"));
        scissorsButton.addActionListener(new SendActionListener("SCISSORS"));
    }

    public void setStatus(String text) {
        statusLabel.setText(text);
    }

    public void loggedIn() {
        setStatus("Logged in!");
        loginButton.setEnabled(false);
        playButton.setEnabled(true);
    }

    public void setScore(String text) {
        scoreLabel.setText(text);
    }

    public void startBattle(String text) {
        setStatus(text);
        playButton.setEnabled(false);
        rockButton.setEnabled(true);
        paperButton.setEnabled(true);
        scissorsButton.setEnabled(true);
    }

    public void setBattleResult(String text) {
        setStatus(text);
        playButton.setEnabled(true);
        rockButton.setEnabled(false);
        paperButton.setEnabled(false);
        scissorsButton.setEnabled(false);
    }

    public void disconnected(String username, String s) {
        setStatus("Disconnected "+username+(s!=null?s : ""));
        playButton.setEnabled(false);
        loginButton.setEnabled(true);
    }

    private class SendActionListener implements ActionListener {
        private final String text;

        public SendActionListener(String text) {
            this.text = text;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            client.answer(text);
            rockButton.setEnabled(false);
            paperButton.setEnabled(false);
            scissorsButton.setEnabled(false);
        }
    }
}
